import {useRouter, useRoute} from "vue-router";
import {onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import admin_service from "../../service/admin_service";

export default function AdminHome() {
    const router = useRouter()
    const route = useRoute()
    let path = ref('/admin/home')
    let language = ref('zh')
    onMounted(() => {
        if (route.path === '/admin' || route.path === '/admin/') {
            router.push('/admin/home')
            path.value = route.path
        }
        path.value = route.path
        if (localStorage.getItem('language') === 'en') {
            language.value = 'en'
        } else {
            language.value = 'zh'
        }
        admin_service.auth()
    })
    const englishBtn = () => {
        if (localStorage.getItem('language') !== 'en') {
            localStorage.setItem('language', 'en')
            router.go(0)
        }
    }
    const chineseBtn = () => {
        if (localStorage.getItem('language') !== 'zh') {
            localStorage.setItem('language', 'zh')
            router.go(0)
        }
    }
    const toHomeBtn = () => {
        router.push('/admin/home').then(() => {
            router.go(0)
        })
    }
    // 菜单列表
    const menuList = ref(
        [
            {
                index: '/admin/home',
                title: '首页',
                en_title: 'Home',
                language: language,
                group: false,
                groupList: []
            },
            {
                index: '/admin',
                title: '文章管理',
                en_title: 'Article management',
                language: language,
                group: true,
                groupList: [
                    {
                        path: '/admin/article_create',
                        title: '新增文章',
                        en_title: 'create article'
                    },
                    {
                        path: '/admin/article_storehouse',
                        title: '文章仓库',
                        en_title: 'Article warehouse'
                    },
                    {
                        path: '/admin/classification',
                        title: '分类管理',
                        en_title: 'Sort management'
                    }
                ]
            },
            {
                index: '/admin/project',
                title: '项目管理',
                en_title: 'Project management',
                language: language,
                group: false,
                groupList: []
            },
            {
                index: '/admin/system',
                title: '系统信息',
                en_title: 'System management',
                language: language,
                group: false,
                groupList: []
            },
        ]
    )
    const toWebHomeBtn = () => {
        router.push('/')
    }

    const loginOutBtn = () => {
        ElMessage.success("退出成功")
        localStorage.removeItem("token")
        localStorage.removeItem("username")
        router.push('/login')
    }
    return {
        toWebHomeBtn,
        englishBtn,
        chineseBtn,
        menuList,
        toHomeBtn,
        path,
        loginOutBtn
    }
}